package cn.hrlly.tao2shou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Applications;
import cn.hrlly.tao2shou.presenter.CancelApplicationPresenter;
import cn.hrlly.tao2shou.presenter.ConfirmApplicationPresenter;
import cn.hrlly.tao2shou.utils.ImageUtil;
import cn.hrlly.tao2shou.view.activity.SellerApplicationActivity;

/**
 * User: gengliang
 * Date: 2016-02-14
 * Time: 09:51
 */

public class SellerApplicationAdapter extends BaseAdapter {

    private ArrayList<Applications> list;
    private CancelApplicationPresenter cancelApplicationPresenter;
    private ConfirmApplicationPresenter confirmApplicationPresenter;
    private SellerApplicationActivity activity;
    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;

    public SellerApplicationAdapter(SellerApplicationActivity activity,ArrayList<Applications> list, CancelApplicationPresenter cancelApplicationPresenter, ConfirmApplicationPresenter confirmApplicationPresenter,Context context) {
        this.list = list;
        this.activity=activity;
        this.cancelApplicationPresenter = cancelApplicationPresenter;
        this.confirmApplicationPresenter = confirmApplicationPresenter;
        imageLoader = ImageLoader.getInstance();
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.layout_seller_application, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        final Applications application=list.get(position);
        if(application!=null){
            imageLoader.displayImage(application.getCommodityPhoto(),holder.ivCommodityPhoto, ImageUtil.getOptions());
            holder.tvCommodityName.setText(application.getCommodityName());
            holder.tvNickname.setText(application.getNickname());
        }
        //点击事件
        holder.btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.applicationId=application.getApplicationId();
                cancelApplicationPresenter.cancelApplication();
            }
        });
        holder.btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.applicationId=application.getApplicationId();
                confirmApplicationPresenter.confirmApplication();
            }
        });
        return convertView;
    }
    static class ViewHolder {
        @Bind(R.id.ivCommodityPhoto)
        ImageView ivCommodityPhoto;
        @Bind(R.id.tvNickname)
        TextView tvNickname;
        @Bind(R.id.tvCommodityName)
        TextView tvCommodityName;
        @Bind(R.id.btnCancel)
        Button btnCancel;
        @Bind(R.id.btnConfirm)
        Button btnConfirm;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
